package com.gmail.spaskhristov.ac;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private String URL;// = "http://10.0.2.2:82/ac/data.txt";
	private String urlSent;
	private final static String EXTRA_MESSAGE_OPTION = "optionAC";
	private final static String EXTRA_MESSAGE_URL = "URL";
	private String optionAC;
	private String msg = "AC project: ";
	private Button btnOnOff;
	private Button btnSetUrl;
	private SharedPreferences prefs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
		this.urlSent = this.prefs.getString("url", null);
		this.URL = this.urlSent + "data.txt";
		if (this.urlSent == null) {
			Intent intent = new Intent(this, SetUrlActivity.class);
			startActivity(intent);
		}

		this.btnOnOff = (Button) findViewById(R.id.btnOnOff);
		this.btnOnOff.setOnClickListener(this);

		this.btnSetUrl = (Button) findViewById(R.id.btnSetUrl);
		this.btnSetUrl.setOnClickListener(this);

		new HttpAsyncTask().execute(URL);

	}

	@Override
	protected void onResume() {
		super.onResume();
		this.urlSent = this.prefs.getString("url", null);
		this.URL = this.urlSent + "data.txt";
		if (this.urlSent == null) {
			Intent intent = new Intent(this, SetUrlActivity.class);
			startActivity(intent);
		}
		Log.d(msg, "The onResume() event");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(msg, "The onPause() event");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(msg, "The onStop() event");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(msg, "The onDestroy() event");
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.btnOnOff:
			intent = new Intent(this, MenuActivity.class);
			intent.putExtra(EXTRA_MESSAGE_OPTION, this.optionAC);
			intent.putExtra(EXTRA_MESSAGE_URL, this.urlSent);
			startActivity(intent);
			break;
		case R.id.btnSetUrl:
			intent = new Intent(this, SetUrlActivity.class);
			startActivity(intent);
			break;
		}
	}

	public String getOptionAC() {
		InputStream inputStream = null;
		String result = "";
		try {
			// create HttpClient
			HttpClient httpclient = new DefaultHttpClient();
			// make GET request to the given URL
			HttpResponse httpResponse = httpclient
					.execute(new HttpGet(this.URL));
			// receive response as inputStream
			inputStream = httpResponse.getEntity().getContent();
			// convert inputstream to string
			if (inputStream != null)
				result = convertInputStreamToString(inputStream);
			else
				result = "Did not work!";

		} catch (Exception e) {
			Log.d("InputStream", e.getLocalizedMessage());
		}
		return result;
	}

	// convert inputstream to String
	private static String convertInputStreamToString(InputStream inputStream)
			throws IOException {
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(inputStream));
		String line = "";
		String result = "";
		while ((line = bufferedReader.readLine()) != null) {
			result += line;
		}
		inputStream.close();
		return result;

	}

	private class HttpAsyncTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			return getOptionAC();
		}

		// onPostExecute displays the results of the AsyncTask.
		@Override
		protected void onPostExecute(String result) {
			optionAC = result;
		}
	}
}
